<?php use_helper('JavascriptBase', 'Tags')  ?>

<?php echo $form['new_tags'] ?>
 
<div class="sf_admin_form_row sf_admin_text sf_admin_form_field_tags">
	<div>
		<div class="content">
			<div class="taglist">   
   				<?php foreach ( $form->getObject()->getTags() as $t):   ?>
    				<span><nobr><?php echo link_to_function("Remove '$t'", "remove_tag(".json_encode($t).", $(this).parent())", "class=removetag") ?>
    				&nbsp;<?php echo $t  ?></nobr></span>
    			<?php endforeach;  ?>
			</div>
			<span id="remove_tag_help" style="display:none;">Tags eliminados. Recuerde guardar los cambios.</span>
		</div>
	</div>
</div>

<script>
$(function() {
        // add fancy jQuery UI button styles.  See additional in "CSS" below
	    $(".taglist a").button({icons:{primary:"ui-icon-trash"}, text: false});
});
 
function remove_tag (tag, element) {
  remove_field = $("#product_remove_tags");
  if ( remove_field.val().length ) {
    remove_field.val( remove_field.val()  + "," + tag );
  }
  else {
    remove_field.val( tag );
  }
  $(element).hide();
  $("#remove_tag_help").show();
}

$(function() {
 
	function split(val) {
		return val.split(/\s*,\s*/);
	}
	function extractLast(term) {
		last = split(term).pop();
		return last;
	}
 
 
	$("#new_tags").autocomplete({
 
		source: function(request, response) {
	      $.getJSON(<?php echo json_encode(url_for("taggableComplete/complete")) ?>, {
				current: extractLast(request.term)
			}, response);
		},
		search: function() {
			// custom minLength
			var term = extractLast(this.value);
			if (term.length < 1) {
				return false;
			}
		},
	    focus: function(event, ui) {
			// prevent value inserted on focus
			return false;
		},
		select: function(event, ui) {
			var terms = split( this.value );
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push( ui.item.value );
			// add placeholder to get the comma-and-space at the end
			terms.push("");
			this.value = terms.join(", ");
			return false;
		}
 	});
});

</script>